﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using LuaInterface;

public class LuaTouchComponent : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler,
												IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler	{


	private Dictionary<EventTriggerType, LuaFunction> functions = new Dictionary<EventTriggerType, LuaFunction>();

	public void RegisterFunction(EventTriggerType triggerType, LuaFunction func) {
		if (func == null) {
			Debug.LogWarning ("EventTriggerType RegisterFunction, params is null!");
			return;
		}

		LuaFunction func1 = null;
		if (functions.TryGetValue (triggerType, out func1)) {
			if (func1 != func) {
				functions [triggerType] = func;
			}
		} else {
			functions.Add (triggerType, func);
		}
	}

	private void OnEventHandle(EventTriggerType triggerType, PointerEventData eventData) {
		LuaFunction func = null;
		if (functions.TryGetValue (triggerType, out func)) {
			//Debug.Log (triggerType);
			func.Call (eventData);
		}
	}

	[NoToLuaAttribute]
	public void OnPointerEnter (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.PointerEnter, eventData);
	}

	[NoToLuaAttribute]
	public void OnPointerExit (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.PointerExit, eventData);
	}

	[NoToLuaAttribute]
	public void OnPointerDown (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.PointerDown, eventData);
	}

	[NoToLuaAttribute]
	public void OnPointerUp (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.PointerUp, eventData);
	}

	[NoToLuaAttribute]
	public void OnPointerClick (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.PointerClick, eventData);
	}

	[NoToLuaAttribute]
	public void OnBeginDrag (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.BeginDrag, eventData);
	}

	[NoToLuaAttribute]
	public void OnDrag (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.Drag, eventData);
	}

	[NoToLuaAttribute]
	public void OnEndDrag (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.EndDrag, eventData);
	}

	[NoToLuaAttribute]
	public void OnDrop (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.Drop, eventData);
	}

	[NoToLuaAttribute]
	public void OnScroll (PointerEventData eventData) {
		OnEventHandle (EventTriggerType.Scroll, eventData);
	}

	//
	void OnDestroy() {
		functions.Clear ();
	}
}
